package games;

import gameUtils.Coordinate;
import games.gameObjects.Grass;
import games.gameObjects.Path;
import games.gameObjects.TDObject;
import games.gameObjects.invaders.Invader;
import games.gameObjects.towers.Tower;

import java.util.Collections;
import java.util.LinkedList;

public class Map {

	private int id;
	private int destroyedInvaders;
	private LinkedList<TDObject> objectsMapList;
	private int invadersToBeCreated;

	public Map(final int id){
		super();
		
		this.id = id;
		this.objectsMapList = new LinkedList<TDObject>();
		this.invadersToBeCreated = getInitialTotalOfInvaders();
	}

	public int getInitialTotalOfInvaders(){
		return id * 10;
	}

	public int getInvadersToBeCreated(){
		return invadersToBeCreated;
	}

	public int getId() {
		return id;
	}

	public int getDestroyedInvaders() {
		return destroyedInvaders;
	}

	public LinkedList<TDObject> getObjectsMapList() {
		return objectsMapList;
	}

	public void add(final TDObject object) {
		objectsMapList.add(object);
	}

	public void remove(final TDObject object) {
		objectsMapList.remove(object);
	}

	public boolean mobsAllCleared() {
		return invadersToBeCreated == 0 && getInvaderList().isEmpty();
	}

	public void incrementDestroyedInvaders(){
		destroyedInvaders++;
	}

	public void decrementInvadersToBeCreated(){
		invadersToBeCreated--;
	}

	public Tower getTowerAt(final Coordinate c){
		for (int i = 0; i < objectsMapList.size(); i++) {
			if(objectsMapList.get(i).getTDClass().equals(Tower.TD_CLASS)){
				if(objectsMapList.get(i).getCoordinate().equals(c)){
					return (Tower) objectsMapList.get(i);
				}
			}
		}
		return null;
	}

	public Invader getInvaderAt(final Coordinate c) {
		for (int i = 0; i < objectsMapList.size(); i++) {
			if(objectsMapList.get(i).getTDClass().equals(Invader.TD_CLASS)){
				if(objectsMapList.get(i).getCoordinate().equals(c)){
					return (Invader) objectsMapList.get(i);
				}
			}
		}
		return null;
	}

	public LinkedList<Path> getPathList(){
		LinkedList<Path> pathList = new LinkedList<Path>();

		for (int i = 0; i < objectsMapList.size(); i++) {
			if(objectsMapList.get(i).getTDClass().equals(Path.TD_CLASS)){
				pathList.add((Path) objectsMapList.get(i));
			}
		}

		Collections.sort(pathList);

		return pathList;
	}


	public Coordinate getNextPathCoordinate(final Coordinate c){
		LinkedList<Path> pathList = getPathList();
		for (int i = 0; i < pathList.size(); i++) {
			if(pathList.get(i).getCoordinate().equals(c)){
				if(i + 1 < pathList.size()){
					return pathList.get(i + 1).getCoordinate();
				}
			}
		}

		return null;
	}

	public LinkedList<Tower> getTowerList(){
		LinkedList<Tower> towerList = new LinkedList<Tower>();

		for (int i = 0; i < objectsMapList.size(); i++) {
			if(objectsMapList.get(i).getTDClass().equals(Tower.TD_CLASS)){
				towerList.add((Tower) objectsMapList.get(i));
			}
		}

		return towerList;
	}

	public LinkedList<Invader> getInvaderList(){
		LinkedList<Invader> invaderList = new LinkedList<Invader>();

		for (int i = 0; i < objectsMapList.size(); i++) {
			if(objectsMapList.get(i).getTDClass().equals(Invader.TD_CLASS)){
				invaderList.add((Invader) objectsMapList.get(i));
			}
		}

		return invaderList;
	}

	public void removeAll(final LinkedList<TDObject> list) {
		objectsMapList.removeAll(list);
	}

	
	public boolean isGrass(final Coordinate c){
		for (int i = 0; i < objectsMapList.size(); i++) {
			if(objectsMapList.get(i).getTDClass().equals(Grass.TD_CLASS)){
				if(objectsMapList.get(i).getCoordinate().equals(c)){
					return true;
				}
			}
		}
		return false;
	}
	
}


